import 'dart:developer';

/// success : true
/// message : "操作成功！"
/// code : 200
/// result : [{"id":1,"novel_id":2,"title":"123","content":"12312","indexx":0,"breakk":0,"create_time":"","update_time":"","del_flag":"0"}]

class ChapterResult {
  bool? _success;
  String? _message;
  int? _code;
  List<Chapter>? _result;

  bool? get success => _success;
  String? get message => _message;
  int? get code => _code;
  List<Chapter>? get result => _result;

  ChapterResult({
      bool? success, 
      String? message, 
      int? code, 
      List<Chapter>? result}){
    _success = success;
    _message = message;
    _code = code;
    _result = result;
}

  ChapterResult.fromJson(dynamic json) {
    _success = json["success"];
    _message = json["message"];
    _code = json["code"];
    if (json["result"] != null) {
      _result = [];
      json["result"]["records"].forEach((v) {
        _result?.add(Chapter.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["success"] = _success;
    map["message"] = _message;
    map["code"] = _code;
    if (_result != null) {
      map["result"] = _result?.map((v) => v.toJson()).toList();
    }
    return map;
  }

}

/// id : 1
/// novel_id : 2
/// title : "123"
/// content : "12312"
/// indexx : 0
/// breakk : 0
/// create_time : ""
/// update_time : ""
/// del_flag : "0"

class Chapter {
  int? _id;
  int? _novelId;
  String? _title;
  String? _content;
  int? _indexx;
  int? _breakk;
  String? _createTime;
  String? _updateTime;
  //总页数
  int? _totalPage;

  set pageOffsets(List<Map<String, int>>? value) {
    _pageOffsets = value;
  }

  String? _delFlag;
  List<Map<String, int>>? _pageOffsets;

  int? get id => _id;
  int? get novelId => _novelId;
  String? get title => _title;
  String? get content => _content;
  int? get indexx => _indexx;
  int? get breakk => _breakk;
  String? get createTime => _createTime;
  String? get updateTime => _updateTime;
  String? get delFlag => _delFlag;
  List<Map<String, int>>? get pageOffsets => _pageOffsets;

  int get pageCount {
    return pageOffsets?.length ?? 0;
  }

  int get totalPage => _totalPage ?? 0;

  set totalPage(int value) {
    _totalPage = value;
  }

  String stringAtPageIndex(int index) {
    var offset = pageOffsets![index];
    if (offset != null) {
      var str = this.content!.substring(offset['start'] ?? 0, offset['end'] ?? 0);
      if (str.startsWith('\n')) {
        str = str.substring(1);
      }
      return str;
    }
    return "";
  }

  Chapter({
      int? id, 
      int? novelId, 
      String? title, 
      String? content, 
      int? indexx, 
      int? breakk, 
      String? createTime, 
      String? updateTime, 
      String? delFlag,
      List<Map<String, int>>? pageOffsets,
      int? totalPage}){
    _id = id;
    _novelId = novelId;
    _title = title;
    _content = content;
    _indexx = indexx;
    _breakk = breakk;
    _createTime = createTime;
    _updateTime = updateTime;
    _delFlag = delFlag;
    _pageOffsets = pageOffsets;
    _totalPage = totalPage;

}

  Chapter.fromJson(dynamic json) {
    _id = json["id"];
    _novelId = json["novelId"];
    _title = json["title"];
    _content = json["content"];
    _indexx = json["indexx"];
    _breakk = json["breakk"];
    _createTime = json["createTime"];
    _updateTime = json["updateTime"];
    _delFlag = json["delFlag"];
    _pageOffsets = json["pageOffsets"];
    _totalPage = json["totalPage"];
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["id"] = _id;
    map["novelId"] = _novelId;
    map["title"] = _title;
    map["content"] = _content;
    map["indexx"] = _indexx;
    map["breakk"] = _breakk;
    map["createTime"] = _createTime;
    map["updateTime"] = _updateTime;
    map["delFlag"] = _delFlag;
    map["pageOffsets"] = _pageOffsets;
    return map;
  }


}